import {defineStore} from "pinia";
import api from "@/api";


//返回值：函数，得到容器示例

export const useProductStore = defineStore('supplier', {
    /**
     * 加入插件后 持久化开启
     */
    persist: true,
    /**
     * 全局状态
     * 函数式 在服务端渲染时避免交叉请求导致数据状态污染
     * 箭头函数 方面TS类型推导
     */
    state: () => {
        return {
            tabIndex: 0,
            tabs: <ITab[]>[{
                url: '/pages/product/home/index',
                icon: 'home',
                title: '首页',
            }, {
                url: '/pages/product/product/index',
                icon: 'grid',
                title: '商品',
            }, {
                url: '/pages/product/supplier/index',
                icon: 'car',
                title: '供应商',
            }, {
                url: '/pages/product/expiry/index',
                icon: 'bell',
                title: '保质期',
            }],

            editMode: 'add',  //编辑页模式 add update

            productList: <IProduct[]>[],   //商品列表
            productTotal: 0,   //商品总数
            selectedProduct: <IProduct>{},

            supplierList: <ISupplier[]>[],   //供应商列表
            supplierTotal: 0,//供应商总数
            selectedSupplier: <ISupplier>{},

            expiryList: <IExpiry[]>[],   //保质期列表
            expiryTotal: 0,//保质期总数
            selectedExpiry: <IExpiry>{},

            categoryList: ['其它', '零食', '饮料',],   //商品分类列表




        }
    },
    /**
     * 封装计算属性，有缓存功能
     */
    getters: {},
    /**
     * 封装业务逻辑，修改state
     */
    actions: {
        updateProduct(obj: IProduct) {
            const index = this.productList.findIndex(item => item.id === obj.id);
            if (index !== -1) {
                this.productList[index] = obj;
            }
        },
        unshiftProduct(obj: IProduct) {
            this.productList.unshift(obj)
            this.productTotal++
        },
        removeProduct(obj: IProduct) {
            const index = this.productList.findIndex(item => item.id === obj.id);
            if (index !== -1) {
                this.productList = this.productList.slice(0, index).concat(this.productList.slice(index + 1));
                this.productTotal--
            }
        },


        unshiftSupplier(obj: ISupplier) {
            this.supplierList.unshift(obj)
            this.supplierTotal++
        },
        updateSupplier(obj: ISupplier) {
            const index = this.supplierList.findIndex(item => item.id === obj.id);
            if (index !== -1) {
                this.supplierList[index] = obj;
            }
        },
        removeSupplier(obj: ISupplier) {
            const index = this.supplierList.findIndex(item => item.id === obj.id);
            if (index !== -1) {
                this.supplierList = this.supplierList.slice(0, index).concat(this.supplierList.slice(index + 1));
                this.supplierTotal--;
            }
        },


        unshiftExpiry(obj: IExpiry) {
            this.expiryList.unshift(obj)
            this.expiryTotal++
        },
        updateExpiry(obj: IExpiry) {
            const index = this.expiryList.findIndex(item => item.id === obj.id);
            if (index !== -1) {
                this.expiryList[index] = obj;
            }
        },
        removeExpiry(obj: IExpiry) {
            const index = this.expiryList.findIndex(item => item.id === obj.id);
            if (index !== -1) {
                this.expiryList = this.expiryList.slice(0, index).concat(this.expiryList.slice(index + 1));
                this.expiryTotal--;
            }
        },
    },
})
